package com.goldplusgold.tradedelay.app.home.live;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.goldplusgold.imageloader.lib.IShow;
import com.goldplusgold.live.LVDataManager;
import com.goldplusgold.live.bean.LDetailBean;
import com.goldplusgold.live.bean.QuestionBean;
import com.example.account.manager.AppCacheSharedPreferences;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.base.constant.ErrorCodeConstant;
import com.goldplusgold.tradedelay.app.base.utils.ActivityUtils;
import com.goldplusgold.tradedelay.app.base.widget.QuitDialog;
import com.example.account.manager.ParmasKey;
import com.goldplusgold.tradedelay.app.home.live.base.LiveBaseCallBack;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/7/17.
 */

public class LiveDetailMiddleControler {

    Context context;
    RelativeLayout live_item_layout_intro;
    ImageView live_item_layout_img_icon;
    TextView description_title_txt;
    TextView live_item_layout_txt_description;
    LinearLayout live_item_layout_detail_follow;
    ImageView live_item_layout_detail_follow_img;
    TextView live_item_layout_detail_follow_txt;
    LDetailBean lDetailBean;
    RelativeLayout middle_live_layout;
    LinearLayout middle_video_layout;
    TextView video_middle_title;
    TextView video_middle_time;
    LinearLayout live_item_linear_layout ;
    boolean likeFlag = false;
    private int type = LiveDetailActivity.LiveDetailType.LIVE;
    public LiveDetailMiddleControler(Context context, RelativeLayout live_item_layout_intro, int type){
        this.context = context;
        this.type = type;
        this.live_item_layout_intro = live_item_layout_intro;
        initView();
    }

    private void initView(){
        middle_live_layout = (RelativeLayout) this.live_item_layout_intro.findViewById(R.id.middle_live_layout);
        middle_video_layout = (LinearLayout) this.live_item_layout_intro.findViewById(R.id.middle_video_layout);
        live_item_layout_detail_follow = (LinearLayout) this.live_item_layout_intro.findViewById(R.id.live_item_layout_detail_follow);
        if(type == LiveDetailActivity.LiveDetailType.LIVE || type == LiveDetailActivity.LiveDetailType.TEXT_LIVE || type == LiveDetailActivity.LiveDetailType.VIDEO_LIVE){
            middle_live_layout.setVisibility(View.VISIBLE);
            live_item_layout_detail_follow = (LinearLayout) this.live_item_layout_intro.findViewById(R.id.live_item_layout_detail_follow);
            live_item_layout_img_icon = (ImageView) this.live_item_layout_intro.findViewById(R.id.live_item_layout_img_icon);
            description_title_txt = (TextView) this.live_item_layout_intro.findViewById(R.id.live_item_layout_txt_name);
            live_item_linear_layout = (LinearLayout) this.live_item_layout_intro.findViewById(R.id.live_item_linear_layout);
            live_item_layout_txt_description = (TextView) this.live_item_layout_intro.findViewById(R.id.live_item_layout_txt_description);
            live_item_layout_detail_follow_img = (ImageView) this.live_item_layout_intro.findViewById(R.id.live_item_layout_detail_follow_img);
            live_item_layout_detail_follow_txt = (TextView) this.live_item_layout_intro.findViewById(R.id.live_item_layout_detail_follow_txt);
            live_item_layout_detail_follow.setVisibility(View.VISIBLE);
            live_item_layout_detail_follow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(likeFlag){
                        changeFollow(false);
                    } else{
                        changeFollow(true);
                    }
                }
            });

            live_item_linear_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //todo 修改了跳转页面
                    Intent intent = new Intent(context, /*ColumnIntroActivity.class*/ColumnInfoActivity.class);
                    intent.putExtra("columnID",lDetailBean.getColumnID());
                    //intent.putExtra("entryType" , ColumnInfoActivity.TYPE_FROM_MIDDLE);
                    context.startActivity(intent);
                }
            });
        }
        if(type == LiveDetailActivity.LiveDetailType.VIDEO){
            middle_video_layout.setVisibility(View.VISIBLE);
            live_item_layout_detail_follow.setVisibility(View.GONE);
            video_middle_title = (TextView) this.live_item_layout_intro.findViewById(R.id.video_middle_title);
            video_middle_time = (TextView) this.live_item_layout_intro.findViewById(R.id.video_middle_time);
        }
    }

    public void setData(LDetailBean lDetailBean){
        this.lDetailBean = lDetailBean;
        refreshView();
    }

    public void setData(String title,String time){
        if(title != null){
            video_middle_title.setText(title);
        }
        if(time != null){
            video_middle_time.setText(time);
        }
    }

    private void refreshView(){
        if(lDetailBean != null){
            if(live_item_layout_img_icon == null){
                return;
            }
            IShow.getInstance().showCircle(context,lDetailBean.getColumnIcon(),live_item_layout_img_icon);
            description_title_txt.setText(lDetailBean.getColumnName());
            live_item_layout_txt_description.setText(lDetailBean.getOneWordIntro());
            refreshFollowView(lDetailBean.getLikeFlag());
        }
    }

    private void changeFollow(final boolean likeFlag){
        if(!AppCacheSharedPreferences.getCacheBoolean(ParmasKey.login_state , false)){
            QuitDialog quitDialog = new QuitDialog(context);
            quitDialog.setMessage("请先登录后关注");
            quitDialog.show();
            return;
        }
        if(likeFlag){
            LVDataManager.getInstance().follow(lDetailBean.getColumnID(), new LiveBaseCallBack<QuestionBean>(context) {
                @Override
                public void onResponse(Call<QuestionBean> call, Response<QuestionBean> response) {
                    super.onResponse(call , response);
                    if(response.body().getStatus()){
                        refreshFollowView(likeFlag);
                        showToast("关注成功");
                    } else {
                        showToast("关注失败");
                    }
                }

                @Override
                public void onFailure(Call<QuestionBean> call, Throwable t) {
                    showToast("关注失败");
                }
            });
        } else{
            LVDataManager.getInstance().unFollow(lDetailBean.getColumnID(), new LiveBaseCallBack<QuestionBean>(context) {
                @Override
                public void onResponse(Call<QuestionBean> call, Response<QuestionBean> response) {
                    super.onResponse(call , response);
                    if(response.body().getStatus()){
                        refreshFollowView(likeFlag);
                        showToast("取消关注成功");
                    } else
                        showToast("取消关注失败");
                }

                @Override
                public void onFailure(Call<QuestionBean> call, Throwable t) {
                    showToast("取消关注失败");
                }
            });
        }
    }

    private void showToast(final String content){
        Toast.makeText(context,content,Toast.LENGTH_SHORT).show();
    }

    private void refreshFollowView(boolean likeFlag){
        this.likeFlag = likeFlag;
        if(this.likeFlag){
            live_item_layout_detail_follow.setBackgroundResource(R.drawable.shape_follow_normal);
            live_item_layout_detail_follow_img.setBackgroundResource(R.drawable.live_icon_follower);
            live_item_layout_detail_follow_txt.setTextColor(context.getResources().getColor(R.color.follower_text));
            live_item_layout_detail_follow_txt.setText(context.getString(R.string.live_ready_follow));
        } else{
            live_item_layout_detail_follow.setBackgroundResource(R.drawable.shape_follow_select);
            live_item_layout_detail_follow_img.setBackgroundResource(R.drawable.live_icon_follow);
            live_item_layout_detail_follow_txt.setTextColor(context.getResources().getColor(R.color.color_gold));
            live_item_layout_detail_follow_txt.setText(context.getString(R.string.live_follow));
        }


    }
}
